package com.mashibing.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.mashibing.common.valid.VList;
import com.mashibing.mall.product.param.AttrGroupRelationParam;
import com.mashibing.mall.product.service.AttrAttrgroupRelationService;
import com.mashibing.mall.product.service.impl.AttrServiceImpl;
import com.mashibing.mall.product.vo.AttrGroupResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.mashibing.mall.product.entity.AttrGroupEntity;
import com.mashibing.mall.product.service.AttrGroupService;
import com.mashibing.common.utils.PageUtils;
import com.mashibing.common.utils.R;

import javax.annotation.Resource;

/**
 * 属性分组 控制器
 *
 * @author ROGzhongyang
 * @email inspiron149@163.com
 * @date 2024-11-18 18:50:24
 */
@RestController
@RequestMapping("product/attrgroup")
    public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private AttrServiceImpl attrService;
    @Resource
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //        @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrGroupService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 属性分组 - 分页
     */
    @RequestMapping("/list/{catelogId}")
    //        @RequiresPermissions("product:attrgroup:list")
    public R page(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 属性分组 - 详情
     */
    @RequestMapping("/info/{attrGroupId}")
    //        @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.detail(attrGroupId);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //        @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //        @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //        @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * 属性分组 - 关联属性列表
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelationList(@PathVariable("attrgroupId") Long attrgroupId){
        return R.ok().put("data", attrService.getRelationAttr(attrgroupId));
    }

    /**
     * 属性分组 - 解除属性关联
     */
    @PostMapping("/attr/relation/delete")
    public R unRelateAttr(@Validated(AttrGroupRelationParam.UnrelateAttr.class) @RequestBody VList<AttrGroupRelationParam> params){
        attrService.unRelateAttr(params);
        return R.ok();
    }

    /**
     * 属性分组 - 查询未关联的属性
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId, @RequestParam Map<String, Object> params){
        PageUtils pageUtils = attrService.getNoAttrRelation(attrgroupId, params);
        return R.ok().put("page", pageUtils);
    }

    /**
     * 属性分组 - 关联属性
     */
    @PostMapping("/attr/relation")
    public R relateAttributes(@RequestBody List<AttrGroupRelationParam> params){
        attrAttrgroupRelationService.relateAttributes(params);
        return R.ok();
    }

    /**
     * 属性分组 - 根据三级分类 id 查询属性信息
     */
    @GetMapping("/{catelogId}/withattr")
    public R getAttrgroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        // 根据三级分类的编号获取对应的属性组和属性组的属性信息
        List<AttrGroupResult> list = attrGroupService
                .getAttrgroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data",list);
    }

}
